package com.yue.yadmin.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import com.yue.yadmin.model.SysMenuDto;
import com.yue.yadmin.model.SysUser;

public class VelocityUtils {
	private static Logger logger = Log.getLogger(VelocityUtils.class);
	private static Map<String,String> buttonIcons = new HashMap<String, String>();;
	public static void setButtonIcon(String url,String icon){
		buttonIcons.put(url, icon);
	}
	public static String getButtonIcon(String url){
		logger.debug("buttonIcons:{},url:{}",buttonIcons,url);
		String icon = buttonIcons.get(url);
		logger.debug("icon:{}",icon);
		return StringUtils.isBlank(icon)?"fa fa-circle-o":icon;
	}
	public static String getDataTablesId(){
		return "dataTables_"+System.currentTimeMillis();
	}
	private static String getAHref(SysMenuDto sysMenuDto){
		String aHref = "javascript:;";
		if(sysMenuDto!=null&&sysMenuDto.getMenu()!=null&&StringUtils.isNotBlank(sysMenuDto.getMenu().getUrl())&&(sysMenuDto.getSubMenus()==null||sysMenuDto.getSubMenus().size()<=0)){
			aHref = SpringContextUtils.getRequest().getContextPath()+sysMenuDto.getMenu().getUrl();
		}
		return aHref;
	}
	public static String getSubMenuHtml(List<SysMenuDto> subMenus,boolean isShow){
		if(subMenus==null||subMenus.size()<=0)return "";
		StringBuilder sb = new StringBuilder();
		sb.append("				<ul class='treeview-menu' "+(isShow?"style='display:block;' ":"")+">");
		for(int i=0,total=subMenus.size();i<total;i++){
			SysMenuDto sysMenuDto = subMenus.get(i);
			sb.append("			<li class='treeview'><a href='"+getAHref(sysMenuDto)+"'><i class='"+sysMenuDto.getMenu().getIcon()+"'></i>");
			sb.append("					<span class='menuName'>"+sysMenuDto.getMenu().getName()+"</span> <span class='pull-right-container'>");
			sb.append("						<i class='fa fa-angle-left pull-right'></i>");
			sb.append("				</span> </a>");
			sb.append(getSubMenuHtml(sysMenuDto.getSubMenus(),false));
			sb.append("			</li>");
		}
		sb.append("				</ul>");
		return sb.toString();
	}
	public static String getMenuHtml(List<SysMenuDto> menus){
		StringBuilder sb = new StringBuilder();
		sb.append("<!-- Left side column. contains the logo and sidebar -->");
		sb.append("<aside class='main-sidebar' id='menu'>");
		sb.append("	<!-- sidebar: style can be found in sidebar.less -->");
		sb.append("	<section class='sidebar'>");
		sb.append("		<!-- Sidebar Menu -->");
		sb.append("		<ul class='sidebar-menu' data-widget='tree'>");
		sb.append("			<li class='header'>菜单</li>");
		sb.append("			<!-- Optionally, you can add icons to the links -->");
		String menuOpen = "menu-open";
		for(int i=0,total=menus.size();i<total;i++){
			SysMenuDto sysMenuDto = menus.get(i);
			sb.append("			<li class='treeview "+(i==0?menuOpen:"")+"'><a href='"+getAHref(sysMenuDto)+"'><i class='"+sysMenuDto.getMenu().getIcon()+"'></i>");
			sb.append("					<span class='menuName'>"+sysMenuDto.getMenu().getName()+"</span> <span class='pull-right-container'>");
			sb.append("						<i class='fa fa-angle-left pull-right'></i>");
			sb.append("				</span> </a>");
			sb.append(getSubMenuHtml(sysMenuDto.getSubMenus(),i==0));
			sb.append("			</li>");
		}
		sb.append("		</ul>");
		sb.append("		<!-- /.sidebar-menu -->");
		sb.append("	</section>");
		sb.append("	<!-- /.sidebar -->");
		sb.append("</aside>");
		return sb.toString();
	}
	public static String getNavbar(){
		StringBuilder sb = new StringBuilder();
		sb.append("<nav class='navbar navbar-static-top' role='navigation'>");
		sb.append("	<!-- Navbar Right Menu -->");
		sb.append("	<div class='navbar-custom-menu'>");
		sb.append("		<ul class='nav navbar-nav'>");
		sb.append("			<!-- Messages: style can be found in dropdown.less-->");
		sb.append("			<li class='dropdown messages-menu'>");
		sb.append("				<!-- Menu toggle button --> ");
		sb.append("				<a href='#' class='dropdown-toggle' data-toggle='dropdown'> ");
		sb.append("					<i class='fa fa-envelope-o'></i> ");
		sb.append("					<span class='label label-success'>4</span>");
		sb.append("				</a>");
		sb.append("				<ul class='dropdown-menu'>");
		sb.append("					<li class='header'>You have 4 messages</li>");
		sb.append("					<li>");
		sb.append("						<!-- inner menu: contains the messages -->");
		sb.append("						<ul class='menu'>");
		sb.append("							<li>");
		sb.append("								<!-- start message --> <a href='#'>");
		sb.append("									<div class='pull-left'>");
		sb.append("										<!-- User Image -->");
		sb.append("										<img src='"+SpringContextUtils.getRequest().getContextPath()+"/static/AdminLTE/dist/img/user2-160x160.jpg' class='img-circle' alt='User Image'>");
		sb.append("									</div> ");
		sb.append("									<!-- Message title and timestamp -->");
		sb.append("									<h4>");
		sb.append("										Support Team <small><i class='fa fa-clock-o'></i> 5mins</small>");
		sb.append("									</h4> <!-- The message -->");
		sb.append("									<p>Why not buy a new awesome theme?</p>");
		sb.append("							</a>");
		sb.append("							</li>");
		sb.append("							<!-- end message -->");
		sb.append("						</ul> <!-- /.menu -->");
		sb.append("					</li>");
		sb.append("					<li class='footer'><a href='#'>See All Messages</a></li>");
		sb.append("				</ul>");
		sb.append("			</li>");
		sb.append("			<!-- /.messages-menu -->");
		sb.append("			<!-- User Account Menu -->");
		sb.append(getUserAccount());
		sb.append("		</ul>");
		sb.append("	</div>");
		sb.append("</nav>");
		return sb.toString();
	}
	private static String getUserAccount(){
		StringBuilder sb = new StringBuilder();
		SysUser sysUser = ShiroUtils.getUserEntity();
		sb.append("<li class='dropdown user user-menu'>");
		sb.append("	<!-- Menu Toggle Button --> ");
		sb.append("	<a href='#' class='dropdown-toggle' data-toggle='dropdown'> <!-- The user image in the navbar-->");
		sb.append("		<img src='"+SpringContextUtils.getRequest().getContextPath()+"/static/AdminLTE/dist/img/user2-160x160.jpg' class='user-image' alt='User Image'> ");
		sb.append("		<!-- hidden-xs hides the username on small devices so only the image appears. -->");
		sb.append("		<span class='hidden-xs'>"+sysUser.getUsername()+"</span>");
		sb.append("	</a>");
		sb.append("	<ul class='dropdown-menu'>");
		sb.append("		<!-- The user image in the menu -->");
		sb.append("		<li class='user-header'>");
		sb.append("			<img src='"+SpringContextUtils.getRequest().getContextPath()+"/static/AdminLTE/dist/img/user2-160x160.jpg' class='img-circle' alt='User Image'>");
		sb.append("			<p>");
		sb.append("				"+sysUser.getUsername()+" - Web Developer ");
		sb.append("				<small>Member since Nov. 2012</small>");
		sb.append("			</p>");
		sb.append("		</li>");
		sb.append("		<!-- Menu Body -->");
		sb.append("		<li class='user-body'>");
		sb.append("			<div class='row'>");
		sb.append("				<div class='col-xs-4 text-center'>");
		sb.append("					<a href='#'>Followers</a>");
		sb.append("				</div>");
		sb.append("				<div class='col-xs-4 text-center'>");
		sb.append("					<a href='#'>Sales</a>");
		sb.append("				</div>");
		sb.append("				<div class='col-xs-4 text-center'>");
		sb.append("					<a href='#'>Friends</a>");
		sb.append("				</div>");
		sb.append("			</div> <!-- /.row -->");
		sb.append("		</li>");
		sb.append("		<!-- Menu Footer-->");
		sb.append("		<li class='user-footer'>");
		sb.append("			<div class='pull-left'>");
		sb.append("				<a href='#' class='btn btn-default btn-flat'>Profile</a>");
		sb.append("			</div>");
		sb.append("			<div class='pull-right'>");
		sb.append("				<a href='javascript:;' class='btn btn-default btn-flat' id='signOut'>Sign out</a>");
		sb.append("			</div>");
		sb.append("		</li>");
		sb.append("	</ul>");
		sb.append("</li>");
		return sb.toString();
	}
}
